Raw python requests against the Qumulo API via REST

This Python example illustrates how raw RESTful requests can be run against the Qumulo API. These patterns could be used if you wish to create other language bindings against Qumulo. While they are one method of using python to interact with Qumulo, we recommend using Qumulo's python bindings installed via **pip install qumulo_api**


In [1]:
import os
import requests
import json
import pprint
# python + ssl on MacOSX is rather noisy against dev clusters
requests.packages.urllib3.disable_warnings()

In [2]:
# set your environment variables or fill in the variables below
API_HOSTNAME = os.environ['API_HOSTNAME'] if 'API_HOSTNAME' in os.environ else '{your-cluster-hostname}'
API_USER =     os.environ['API_USER']     if 'API_USER'     in os.environ else '{api-cluster-user}'
API_PASSWORD = os.environ['API_PASSWORD'] if 'API_PASSWORD' in os.environ else '{api-cluster-password}'

In [3]:
# Setting up URLs and default header parameters
root_url = 'https://' + API_HOSTNAME + ':8000'

who_am_i_url   = root_url + '/v1/session/who-am-i'
login_url      = root_url + '/v1/session/login'

default_header = {'content-type': 'application/json'}

Login to the Qumulo cluster via a "POST" to the login_url


In [4]:
post_data = {'username': API_USER, 'password': API_PASSWORD}

resp = requests.post(login_url, 
                  data=json.dumps(post_data), 
                  headers=default_header, 
                  verify=False)

resp_data = json.loads(resp.text)

# Print the response for the login attempt.
pprint.pprint(resp_data)


{u'algorithm': u'hmac-sha-256',
 u'bearer_token': u'1:AUQAAABlSnlyZkhrcmdnRUlKTXR1ZzJrRElQN0N5QUFHakV3UW1obUpEZVBEMUxBRE1Sc1F4eVNtNUdibStRRlpBTVZjQ1QwPSAAAAA/LxgDRrzlezJ/f4wjsWThfQ297qKS4AgaG9E1pz/HmA==',
 u'key': u'Py8YA0a85Xsyf3+MI7Fk4X0Nve6ikuAIGhvRNac/x5g=',
 u'key_id': u'eJyrfHkrggEIJMtug2kDIP7CyAAGjEwQmhmJDePD1LADMRsQxySm5Gbm+QFZAMVcCT0='}

Set up the Authorization bearer token header


In [5]:
default_header['Authorization'] = 'Bearer ' + resp_data['bearer_token']

# A look at the current default requests header now
pprint.pprint(default_header)


{'Authorization': u'Bearer 1:AUQAAABlSnlyZkhrcmdnRUlKTXR1ZzJrRElQN0N5QUFHakV3UW1obUpEZVBEMUxBRE1Sc1F4eVNtNUdibStRRlpBTVZjQ1QwPSAAAAA/LxgDRrzlezJ/f4wjsWThfQ297qKS4AgaG9E1pz/HmA==',
 'content-type': 'application/json'}

Run who am I via a raw "GET" request


In [6]:
resp = requests.get(who_am_i_url, 
                  headers=default_header, 
                  verify=False)

# Print the response. Include the id, sid, and uid
pprint.pprint(json.loads(resp.text))


{u'id': u'500',
 u'name': u'admin',
 u'primary_group': u'513',
 u'sid': u'S-1-5-21-2200309403-3182498638-1868133574-500',
 u'uid': u'0'}

In [ ]: